package com.bjc.platform.spring.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.bjc.platform.model.CustomUserDetails;
import com.bjc.platform.util.StaticMethod;

public class SpringMVCInterceptor extends HandlerInterceptorAdapter {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		CustomUserDetails user = StaticMethod.getUser();
		String uri = request.getRequestURI();
		uri = uri.replace("//", "/").replace(request.getContextPath(), "");
		List<Object> urlRes = user.getUrlRes();

		if (urlRes != null && urlRes.size() > 0) { // 检查访问的资源和权限
			for (Object res : urlRes) {
				if (uri.startsWith(((String) res).replace("//", "/"))) {
					return true;
				}
			}
			response.setStatus(403); // 表示服务器拒绝提供相关的资源
			return false;
		}

		return true;
	}
}
